import { createPinia,defineStore } from "pinia";
import type { UserState } from "@/stores/interface";
import piniaPersistConfig from "@/stores/helper/persist";

export const useUserStore = defineStore({
  id: "user",
  state: (): UserState => ({
    token: "",
    userInfo:  {
      id: 0,
      account: '',
      nickname: '',
      realname: '',
      gender: 0,
      phone: '',
      role: 0,
      status: 0,
      avatar: '',
      email: '', // 如果需要的话
    },
  }),
  getters: {


  },
  actions: {
    // Set Token
    setToken(token: string) {
      this.token = token;

    },
    // Set setUserInfo
    setUserInfo(userInfo: UserState["userInfo"]) {
      this.userInfo = userInfo;
    },

    // 退出登录
    logout() {
      // // 清除 store 中的 token 和 userInfo
      // this.token = "";
      // this.userInfo = {
      //   id: 0,
      //   account: '',
      //   nickname: '',
      //   realname: '',
      //   gender: 0,
      //   phone: '',
      //   role: 0,
      //   status: 0,
      //   avatar: '',
      //   email: '', // 如果需要的话
      // };
      // 从 localStorage 中移除 token
      localStorage.removeItem("user");
    },
  },
  persist: piniaPersistConfig("user")
});
